package com.sanjiahantang.youjiarole.module.common.models.impl;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.common.beans.ShopBean;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.models.IShopModel;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiarole.module.match.beans.MatchBean;
import com.sanjiahantang.youjiarole.module.match.beans.MatchListBean;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by zy on 2017/9/5.
 */

public class ShopModel implements IShopModel , StaticValue {
    @Override
    public void getShopData(final Context context, final CallBack<ShopBean> callBack) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("roleId", SPUtils.get(context, SPConfig.ROLE_ID,"").toString());

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/mall/phone/goodsPage/indexGoodsPageInfoForRole",param,
                new BaseDelegate.ResultCallback<StringBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                            try {
                                JSONObject jsonObject = new JSONObject(response.getData());
                                JSONObject categoryFullList = jsonObject.getJSONObject("categoryFullList");
                                ShopBean bean = new ShopBean();
                                Iterator it = categoryFullList.keys();
                                List<ShopBean.Category> categoryList = new ArrayList<ShopBean.Category>();
                                Gson gson = new Gson();
                                while (it.hasNext()) {
                                    String key = (String) it.next();
                                    JSONArray array = categoryFullList.getJSONArray(key);
                                    List<ShopBean.CategoryItem> items = new ArrayList<ShopBean.CategoryItem>();
                                    for(int i=0;i<array.length();i++){
                                        ShopBean.CategoryItem categoryItem = gson.fromJson(array.getString(i), ShopBean.CategoryItem.class);
                                        items.add(categoryItem);
                                    }
                                    ShopBean.Category category = new ShopBean.Category();
                                    category.setName(key);
                                    category.setItemList(items);
                                    categoryList.add(category);
                                }
                                bean.setCategoryFullList(categoryList);
                                List<ShopBean.Category> categoryList1 = gson.fromJson(jsonObject.getString("categoryList"),new TypeToken<List<ShopBean.Category>>(){}.getType());
                                bean.setCategoryList(categoryList1);

                                List<ShopBean.Advisement> adList = gson.fromJson(jsonObject.getString("goodsPageAdList"),new TypeToken<List<ShopBean.Advisement>>(){}.getType());
                                bean.setGoodsPageAdList(adList);

                                List<MatchListBean.Detail> details = gson.fromJson(jsonObject.getString("togeterList"),new TypeToken<List<MatchListBean.Detail>>(){}.getType());
                                bean.setTogeterList(details);
                                callBack.onSuccess(bean,context, tag);
                            } catch (JSONException e) {
                                e.printStackTrace();
                                callBack.onFailure(e.getMessage());
                            }
                        }else {
                            callBack.onSuccess(null,context,tag);
                        }
                    }

                });

    }
}
